#!/bin/bash
# Filename: udevbyuuid.sh
# a script for create udev rules by scsi device's UUID, For redflag linux only
# If the uuidfile is not given, The script will create rules by get the current scsi device UUID.
# Please make sure sysfs has mounted.

# check your permission
if [ $UID != 0 ]; then
    echo "Please run this script as root!"
    exit 60
fi

# check the udev rules' files
TARGETFILE=/etc/udev/rules.d/20-names.rules
if [ -f $TARGETFILE ]; then
	echo "$TARGETFILE already exists, please remove it first!"
	exit 1
fi

# get the uuid from system
SSIDCMD="scsi_id -g -s"
for node in `ls /sys/block/sd* -d | awk -F"/" '{print FS$3FS$4}'`; do
    unset SCSIID
    unset NODENAME
    echo $node
    SCSIID=`$SSIDCMD $node`
    NODENAME=`echo $node | awk -F"/" '{print $3}'`
    echo "KERNEL="sd*",BUS="scsi",PROGRAM="/sbin/scsi_id",RESULT="$SCSIID",NAME="$NODENAME%n"" >> $TARGETFILE
done
echo "Successed! You can modify the $TARGETFILE, as your choice."
exit 0
